package com.gqj.bas.service.impl;

import com.gqj.bas.domain.BasCabinetGrid;
import com.gqj.bas.mapper.BasCabinetGridMapper;
import com.gqj.bas.service.IBasCabinetGridService;
import com.gqj.common.constant.UserConstants;
import com.gqj.common.exception.ServiceException;
import com.gqj.common.utils.DateUtils;
import com.gqj.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 层格Service业务层处理
 * 
 * @author lxq
 * @date 2023-12-17
 */
@Service
public class BasCabinetGridServiceImpl implements IBasCabinetGridService 
{
    @Autowired
    private BasCabinetGridMapper basCabinetGridMapper;

    /**
     * 查询层格
     * 
     * @param gridId 层格主键
     * @return 层格
     */
    @Override
    public BasCabinetGrid selectBasCabinetGridByGridId(Long gridId)
    {
        return basCabinetGridMapper.selectBasCabinetGridByGridId(gridId);
    }

    /**
     * 查询层格列表
     * 
     * @param basCabinetGrid 层格
     * @return 层格
     */
    @Override
    public List<BasCabinetGrid> selectBasCabinetGridList(BasCabinetGrid basCabinetGrid)
    {
        return basCabinetGridMapper.selectBasCabinetGridList(basCabinetGrid);
    }

    /**
     * 新增层格
     * 
     * @param basCabinetGrid 层格
     * @return 结果
     */
    @Override
    public int insertBasCabinetGrid(BasCabinetGrid basCabinetGrid)
    {
        BasCabinetGrid info = basCabinetGridMapper.selectBasCabinetGridByGridId(basCabinetGrid.getParentId());
        if (null != info) {
            // 如果父节点不为正常状态,则不允许新增子节点
            if (!UserConstants.DEPT_NORMAL.equals(info.getDelFlag())) {
                throw new ServiceException("父级已被删除，不允许新增");
            }
            basCabinetGrid.setAncestors(info.getAncestors() + "," + basCabinetGrid.getParentId());
        } else {
            basCabinetGrid.setAncestors(basCabinetGrid.getParentId().toString());
        }
        basCabinetGrid.setCreateTime(DateUtils.getNowDate());
        return basCabinetGridMapper.insertBasCabinetGrid(basCabinetGrid);
    }

    /**
     * 修改层格
     * 
     * @param basCabinetGrid 层格
     * @return 结果
     */
    @Override
    public int updateBasCabinetGrid(BasCabinetGrid basCabinetGrid)
    {
        BasCabinetGrid newParentGrid = basCabinetGridMapper.selectBasCabinetGridByGridId(basCabinetGrid.getParentId());
        BasCabinetGrid oldGrid = basCabinetGridMapper.selectBasCabinetGridByGridId(basCabinetGrid.getGridId());
        if (StringUtils.isNotNull(newParentGrid) && StringUtils.isNotNull(oldGrid)) {
            String newAncestors = newParentGrid.getAncestors() + "," + newParentGrid.getGridId();
            String oldAncestors = oldGrid.getAncestors();
            basCabinetGrid.setAncestors(newAncestors);
            updateSortChildren(basCabinetGrid.getGridId(), newAncestors, oldAncestors);
        } else if(StringUtils.isNull(newParentGrid)){
            basCabinetGrid.setAncestors(basCabinetGrid.getParentId().toString());
            String oldAncestors = oldGrid.getAncestors();
            updateSortChildren(basCabinetGrid.getGridId(), basCabinetGrid.getParentId().toString(), oldAncestors);
        }
        basCabinetGrid.setUpdateTime(DateUtils.getNowDate());
        return basCabinetGridMapper.updateBasCabinetGrid(basCabinetGrid);
    }

    /**
     * 修改子元素关系
     *
     * @param gridId       被修改的层格ID
     * @param newAncestors 新的父ID集合
     * @param oldAncestors 旧的父ID集合
     */
    public void updateSortChildren(Long gridId, String newAncestors, String oldAncestors) {
        List<BasCabinetGrid> children = basCabinetGridMapper.selectChildrenGridById(gridId);
        for (BasCabinetGrid child : children) {
            child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
        }
        if (children.size() > 0) {
            basCabinetGridMapper.updateGridChildren(children);
        }
    }

    /**
     * 批量删除层格
     * 
     * @param gridIds 需要删除的层格主键
     * @return 结果
     */
    @Override
    public int deleteBasCabinetGridByGridIds(Long[] gridIds)
    {
        return basCabinetGridMapper.deleteBasCabinetGridByGridIds(gridIds);
    }

    /**
     * 删除层格信息
     * 
     * @param gridId 层格主键
     * @return 结果
     */
    @Override
    public int deleteBasCabinetGridByGridId(Long gridId)
    {
        return basCabinetGridMapper.deleteBasCabinetGridByGridId(gridId);
    }

    /**
     * 是否存在子节点
     *
     * @param gridId 层ID
     * @return 结果
     */
    @Override
    public boolean hasChildByGridId(Long gridId)
    {
        int result = basCabinetGridMapper.hasChildByGridId(gridId);
        return result > 0;
    }

    /**
     * 查询分类是否被引用
     *
     * @param gridId 层ID
     * @return 结果 true 存在 false 不存在
     */
    @Override
    public boolean checkMaterialExistGrid(Long gridId)
    {
        int result = basCabinetGridMapper.checkMaterialExistGrid(gridId);
        return result > 0;
    }

    /**
     * 查询层格下拉框
     *
     * @param basCabinetGrid 层格
     * @return 层格集合
     */
    @Override
    public List<Map<String, Object>> gridOptions(BasCabinetGrid basCabinetGrid) {
        return basCabinetGridMapper.gridOptions(basCabinetGrid);
    }
}
